#include "analytedatabase.h"

EleAttributeBase::EleAttributeBase(QString name,double massNum,double abundance)
{
    nucleus=1;//原子核
    this->name=name;
    this->massNum=massNum;//质量数
    this->abundance=abundance;//丰度
    formula="";//方程式
    interferRef="";//干扰参考
    bIsUsed=true;//true:实验调用界面使能 出现
    bIsDefault=false;//?
    bIsChoose=false;//非谱线界面使用
    bIsForbid=false;//true:forbid
}

//
EleAttributes::EleAttributes()
{
    EleAttributeBase data;
    //datas.append(data);
    name="";//
    rsf=1;
    atomicNum=1;
    energyFirst=0;
    energySecond=0;
}
QHash<QString,EleAttributeBase> EleAttributes::GetEleAttributes()
{
   return datas;
}
void EleAttributes::InsertElement(QString ele,EleAttributeBase attribute)
{
    datas.insert(ele,attribute);
}
void EleAttributes::RemoveElement(QString ele,EleAttributeBase attribute)
{
    datas.remove(ele);
}
void EleAttributes::SetElement(QString ele,EleAttributeBase attribute)
{
    datas[ele]=attribute;
}
void EleAttributes::SetRsf(double value)
{
    this->rsf=value;
}
void EleAttributes::SetAtomicNum(double value)
{
   this->atomicNum=value;
}
void EleAttributes::SetEnergyFirst(double value)
{
    this->energyFirst=value;
}
void EleAttributes::SetEnergySecond(double value)
{
    this->energySecond=value;
}

//
CalRatio::CalRatio()
{
    name="test";
    dateTime=QDateTime::currentDateTime();
    for(int i=0;i<5;i++)
    {
        ratios.append(0);
    }
}

SweepScanPara::SweepScanPara()
{
    startMassNum=1;
    endMassNum=2;
    dwelltime=0.3;//0.6 0.3
    channelsAmu=25;//10 25
    type="标准";

    bIsUsed=true;
    bIsDisplay=true;
    //QColor color;
}
void SweepScanPara::SetStartEnd(double start,double end)
{
    startMassNum=start;
    endMassNum=end;
}
bool SweepScanPara::CheckValid()
{
    if(startMassNum>=endMassNum||channelsAmu==0) return false;
    return true;
}

HopScanPara::HopScanPara()
{
    name="质量数+元素";//名称（质量数+元素）：质量数取整
    dwelltime=10;
    chanels=3;
    interval=0.02;
    type="标准";

    bIsUsed=true;
    bIsDisplay=true;
    //QColor color;
}
bool HopScanPara::CheckValid()
{
    if(chanels%2==0) return false;
    return true;
}
//ExpConllectPara
ExpConllectPara::ExpConllectPara()
{
    bPreScan=false;
    preScanCount=10;
    preScanTime=0;

    bScan=false;
    scanCount=10;
    mainScanTime=0;

    //test
    SweepScanPara sweepScanPara;
    HopScanPara hopScanPara;
    //preSweepScanParas.insert("test",sweepScanPara);
    //sweepScanParas.insert("test",sweepScanPara);
    //hopScanParas.insert("test",hopScanPara);
}
bool ExpConllectPara::EnablePreScan(bool state)
{
    this->bPreScan=state;
    return true;
}
bool ExpConllectPara::IsPreScan()
{
    return this->bPreScan;
}

bool ExpConllectPara::SetPreScanCount(int count)
{
    this->preScanCount=count;
    return true;
}
int ExpConllectPara::GetPreScanCount()
{
    return this->preScanCount;
}

double ExpConllectPara::GetPreScanTime()
{
    return this->preScanTime;
}

QList<SweepScanPara> ExpConllectPara::GetPreSweepScanParas()
{
    return this->preSweepScanParas;
}
SweepScanPara ExpConllectPara::GetPreSweepScanPara(int index)
{
    return this->preSweepScanParas.at(index);
}
void ExpConllectPara::SetPreSweepScanPara(int index,SweepScanPara para)
{
    this->preSweepScanParas[index]=para;//
}
void ExpConllectPara::SetPreSweepScanParas(QList<SweepScanPara> paras)
{
    preSweepScanParas=paras;
}
void ExpConllectPara::InitPreScanPara(QList<QPair<double,double> > pairs)
{
    preSweepScanParas.clear();
    for(int i=0;i<pairs.count();i++)
    {
        SweepScanPara sweepScanPara;
        sweepScanPara.SetStartEnd(pairs.at(i).first,pairs.at(i).second);
        preSweepScanParas.append(sweepScanPara);
    }
}

bool ExpConllectPara::EnableScan(bool state)
{
    this->bScan=state;
    return true;
}
bool ExpConllectPara::IsScan()
{
    return this->bScan;
}

bool ExpConllectPara::SetScanCount(int count)
{
    this->scanCount=count;
    return true;
}
int ExpConllectPara::GetScanCount()
{
    return this->scanCount;
}

double ExpConllectPara::GetScanTime()
{
    return this->mainScanTime;
}

QHash<QString,SweepScanPara> ExpConllectPara::GetSweepScanParas()
{
    return this->sweepScanParas;
}
SweepScanPara ExpConllectPara::GetSweepScanPara(QString index)
{
    return this->sweepScanParas.value(index);
}
bool ExpConllectPara::SetSweepScanParas(QHash<QString,SweepScanPara> paras)
{
    sweepScanParas=paras;
}
bool ExpConllectPara::SetSweepScanPara(QString index,SweepScanPara sweepScanPara)
{
    sweepScanParas[index]=sweepScanPara;
    return true;
}
bool ExpConllectPara::RemoveSweepScanPara(QString index)
{
    sweepScanParas.remove(index);
    return true;
}
bool ExpConllectPara::AddSweepScanPara(QString index,SweepScanPara sweepScanPara)
{
    sweepScanParas.insert(index,sweepScanPara);
    return true;
}

QHash<QString,HopScanPara> ExpConllectPara::GetHopScanParas()
{
    return this->hopScanParas;
}
HopScanPara ExpConllectPara::GetHopScanPara(QString str)
{
    return this->hopScanParas.value(str);
}
bool ExpConllectPara::SetHopScanPara(QString name,HopScanPara hopScanPara)
{
    hopScanParas[name]=hopScanPara;
    return true;
}
bool ExpConllectPara::AddHopScanPara(QString name,HopScanPara hopScanPara)
{
    if(hopScanParas.contains(name)) return false;
    hopScanParas.insert(name,hopScanPara);
    return true;
}
bool ExpConllectPara::RemoveHopScanPara(QString name)
{
    hopScanParas.remove(name);
    return true;
}

void ExpMethod::InitMethod(AnalyteDataBase *pAnalyteDataBase)
{
    for(int i=0;i<3;i++)
    {
        foreach(EleAttributes eleAttributes,pAnalyteDataBase->GetElements(i))
        {
            foreach(EleAttributeBase base,eleAttributes.GetEleAttributes())
            {
               if(eleMethods.contains(base.name)) continue;//

               EleMethod eleMethod;
               eleMethod.InitStartEnd(base.massNum,base.massNum-0.1,base.massNum+0.1);//初始化
               eleMethods.insert(base.name,eleMethod);
            }
        }
    }
}
bool ExpMethod::SetMethod(QString name,EleMethod eleMethod)
{
    //eleMethods[name]=eleMethod;
    eleMethods.insert(name,eleMethod);
    return true;
}
EleMethod ExpMethod::GetEleMethod(QString name)
{
    return eleMethods.value(name);
}
EleMethod *ExpMethod::GetEleMethodPtr(QString name)
{
    return &eleMethods[name];
}
QHash<QString,EleMethod> ExpMethod::GetMethods()
{
    return eleMethods;
}


//
AnalyteDataBase::AnalyteDataBase()
{
    Init();
}

void AnalyteDataBase::Init()
{
    elementList<<QStringList()<<"Bkg"<<"H"<<"He"<<"Li"<<"Be"<<"B"<<"C"<<"N"<<"O"<<"F"<<"Ne"
                              <<"Na"<<"Mg"<<"Al"<<"Si"<<"P"<<"S"<<"Cl"<<"Ar"<<"K"<<"Ca"
                              <<"Sc"<<"Ti"<<"V"<<"Cr"<<"Mn"<<"Fe"<<"Co"<<"Ni"<<"Cu"<<"Zn"
                              <<"Ga"<<"Ge"<<"As"<<"Se"<<"Br"<<"Kr"<<"Rb"<<"Sr"<<"Y"<<"Zr"
                              <<"Nb"<<"Mo"<<"Tc"<<"Ru"<<"Rh"<<"Pd"<<"Ag"<<"Cd"<<"In"<<"Sn"
                              <<"Sb"<<"Te"<<"I"<<"Xe"<<"Cs"<<"Ba"<<"La"<<"Ce"<<"Pr"<<"Nd"
                              <<"Pm"<<"Sm"<<"Eu"<<"Gd"<<"Tb"<<"Dy"<<"Ho"<<"Er"<<"Tm"<<"Yb"
                              <<"Lu"<<"Hf"<<"Ta"<<"W"<<"Re"<<"Os"<<"Ir"<<"Pt"<<"Au"<<"Hg"
                              <<"Ti"<<"Pb"<<"Bi"<<"Po"<<"At"<<"Rn"<<"Fr"<<"Ra"<<"Ac"<<"Th"
                              <<"Pa"<<"U""Np"<<"Pu"<<"Am"<<"Cm"<<"Bk"<<"Cf"<<"Es"<<"Fm"
                              <<"Md"<<"No"<<"Lw";
    elementBaseList<<QStringList()<<"Ar"<<"O"<<"N"<<"C"<<"H"<<"O H";
    elementMutiList<<QStringList()<<"Ba++"<<"O H"<<"Ar Cl"<<"Ce O"<<"Ba O";

    for(int i=0;i<elementList.count();i++)
    {
        EleAttributes eleAttributes;
        elementTable.insert(elementList.at(i),eleAttributes);
    }
    for(int i=0;i<elementBaseList.count();i++)
    {
        EleAttributes eleAttributes;
        elementBaseTable.insert(elementBaseList.at(i),eleAttributes);
    }
    for(int i=0;i<elementMutiList.count();i++)
    {
        EleAttributes eleAttributes;
        elementMutiTable.insert(elementMutiList.at(i),eleAttributes);
    }

    forbidRangs<<qMakePair(0.0,4.95)
               <<qMakePair(11.41,22.59)
               <<qMakePair(27.41,28.59)
               <<qMakePair(29.41,30.59)
               <<qMakePair(31.41,32.59)
               <<qMakePair(33.41,38.59)
               <<qMakePair(39.41,42.59)
               <<qMakePair(79.41,80.59)
               <<qMakePair(245.5,260.0);
}

double AnalyteDataBase::GetElementMass(QString name)
{
    for(int i=0;i<3;i++)
    {
        foreach(EleAttributes eleAttributes,GetElements(i))
        {
            foreach(EleAttributeBase base,eleAttributes.GetEleAttributes())
            {
                if(base.name==name)
                {
                    return base.massNum;
                }
            }
        }
    }
    return 0;//false
}

QHash<QString,EleAttributes> AnalyteDataBase::GetElements(int type)
{
    switch(type)
    {
    case 0:
        return elementTable;
        break;
    case 1:
        return elementMutiTable;
        break;
    case 2:
        return elementBaseTable;
        break;
    }
    return elementTable;
}

QHash<QString,EleAttributes> AnalyteDataBase::GetElementTable()
{
    return elementTable;
}
QHash<QString,EleAttributes> AnalyteDataBase::GetElementMutiTable()
{
   return elementMutiTable;
}
QHash<QString,EleAttributes> AnalyteDataBase::GetElementBaseTable()
{
   return elementBaseTable;
}
QHash<QString,EleAttributes> AnalyteDataBase::GetAnalyteDatas()
{
    return analyteDatas;
}
QList<QPair<double,double> > AnalyteDataBase::GetForbidRangs()
{
   return forbidRangs;
}
QList<QPair<double,double> > AnalyteDataBase::GetEnableRangs()
{
    QList<QPair<double,double> > enableRangs;
    int count=forbidRangs.count();
    if(count<2) return enableRangs;
    for(int i=1;i<count;i++)
    {
       QPair<double,double>  pairs;
       pairs.first=forbidRangs.at(i-1).second;
       pairs.second=forbidRangs.at(i).first;
       enableRangs.append(pairs);
    }

    return enableRangs;
}

QStringList AnalyteDataBase::GetAnalyteElements()
{
    QStringList elements;
    foreach(EleAttributes eleAttributes,elementTable)
    {
        foreach(EleAttributeBase base,eleAttributes.GetEleAttributes())
        {
            if(base.bIsChoose)
            {
               elements.append(base.name);
            }
        }
    }
    foreach(EleAttributes eleAttributes,elementMutiTable)
    {
        foreach(EleAttributeBase base,eleAttributes.GetEleAttributes())
        {
            if(base.bIsChoose)
            {
               elements.append(base.name);
            }
        }
    }
    foreach(EleAttributes eleAttributes,elementBaseTable)
    {
        foreach(EleAttributeBase base,eleAttributes.GetEleAttributes())
        {
            if(base.bIsChoose)
            {
               elements.append(base.name);
            }
        }
    }

    return elements;
}
QMap<double,QString> AnalyteDataBase::GetAnalyteElementPairs()
{
    QMap<double,QString> elements;
    foreach(EleAttributes eleAttributes,elementTable)
    {
        foreach(EleAttributeBase base,eleAttributes.GetEleAttributes())
        {
            if(base.bIsChoose)
            {
               elements.insert(base.massNum,base.name);
            }
        }
    }
    foreach(EleAttributes eleAttributes,elementMutiTable)
    {
        foreach(EleAttributeBase base,eleAttributes.GetEleAttributes())
        {
            if(base.bIsChoose)
            {
               elements.insert(base.massNum,base.name);
            }
        }
    }
    foreach(EleAttributes eleAttributes,elementBaseTable)
    {
        foreach(EleAttributeBase base,eleAttributes.GetEleAttributes())
        {
            if(base.bIsChoose)
            {
               elements.insert(base.massNum,base.name);
            }
        }
    }

    return elements;
}
void AnalyteDataBase::InsertEleAttributes(EleAttributes attribute,QString element,int type)
{
    switch(type)
    {
    case 0:
        elementTable.insert(element,attribute);
        break;
    case 1:
        elementMutiTable.insert(element,attribute);
        break;
    case 2:
        elementBaseTable.insert(element,attribute);
        break;
    }
}
void AnalyteDataBase::RemoveEleAttributes(EleAttributes attribute,QString element,int type)
{
    switch(type)
    {
    case 0:
         elementTable.remove(element);
        break;
    case 1:
         elementMutiTable.remove(element);
        break;
    case 2:
         elementBaseTable.remove(element);
        break;
    }
}
void AnalyteDataBase::SetEleAttributes(EleAttributes attribute,QString element,int type)
{
    switch(type)
    {
    case 0:
        elementTable[element]=attribute;
        break;
    case 1:
         elementMutiTable[element]=attribute;
        break;
    case 2:
         elementBaseTable[element]=attribute;
        break;
    }
}

void AnalyteDataBase::InsertEleAttribute(QString massElement,EleAttributeBase isotope,QString element,int type)
{
    switch(type)
    {
    case 0:
        elementTable[element].InsertElement(massElement,isotope);
        break;
    case 1:
        elementMutiTable[element].InsertElement(massElement,isotope);
        break;
    case 2:
        elementBaseTable[element].InsertElement(massElement,isotope);
        break;
    }
}
void AnalyteDataBase::RemoveEleAttribute(QString massElement,EleAttributeBase isotope,QString element,int type)
{
    switch(type)
    {
    case 0:
         elementTable[element].RemoveElement(massElement,isotope);
        break;
    case 1:
         elementMutiTable[element].RemoveElement(massElement,isotope);
        break;
    case 2:
         elementBaseTable[element].RemoveElement(massElement,isotope);
        break;
    }
}
void AnalyteDataBase::SetEleAttribute(QString massElement,EleAttributeBase isotope,QString element,int type)
{
    switch(type)
    {
    case 0:
         elementTable[element].SetElement(massElement,isotope);
        break;
    case 1:
         elementMutiTable[element].SetElement(massElement,isotope);
        break;
    case 2:
         elementBaseTable[element].SetElement(massElement,isotope);
        break;
    }
}
void AnalyteDataBase::SetEleRsf(double rsf,QString element,int type)
{
   elementTable[element].SetRsf(rsf);
}
void AnalyteDataBase::SetEleAtomicNum(double atomicNum,QString element,int type)
{
    elementTable[element].SetAtomicNum(atomicNum);
}
void AnalyteDataBase::SetEleEnergyFirst(double energyFirst,QString element,int type)
{
    elementTable[element].SetEnergyFirst(energyFirst);
}
void AnalyteDataBase::SetEleEnergySecond(double energySecond,QString element,int type)
{
    elementTable[element].SetEnergySecond(energySecond);
}


//
WorkFlow::WorkFlow()
{
//    QList<WorkStep> workSteps;
//    flows.insert("name1",workSteps);
//    WorkStep step;
//    workSteps.append(step);
//    flows.insert("name2",workSteps);
//    flows.insert("name3",workSteps);
}
QList<WorkStep>  WorkFlow::GetWorkSteps(QString name)
{
    return flows.value(name);
}
QList<QString> WorkFlow::GetFlowNames()
{
    return flows.keys();
}
void WorkFlow::InsetFlow(QString name,QList<WorkStep> steps)
{
    flows.insert(name,steps);
}
void WorkFlow::RemoveFlow(QString name)
{
    flows.remove(name);
}
void WorkFlow::InsetStep(QString name,int index,WorkStep step)
{
    flows[name].insert(index,step);
}
void WorkFlow::Replace(QString name,int index,WorkStep step)
{
    flows[name].replace(index,step);
}
void WorkFlow::AddStep(QString name,WorkStep step)
{
    flows[name].append(step);
}
void WorkFlow::RemoveStep(QString name,int index)
{
    if(flows.contains(name)==false) return;
    if(flows.value(name).count()<=index) return;
    flows[name].removeAt(index);
}
void WorkFlow::Clear()
{
    flows.clear();
}
bool WorkFlow::IsExistFlow(QString name)
{
    return flows.contains(name);
}
